<?php
global $wpdb;
require_once('./wp-admin/includes/user.php');

$blogusers = get_users_of_blog(); /* Henter alle registrerte brukere */
foreach ($blogusers as $bloguser) {

    $user = get_userdata($bloguser->user_id); /* Henter data om hver enkelt bruker som et objekt */
    $instr = $wpdb->get_row("
           SELECT feltdata, indeks
           FROM feltdata
           WHERE feltdataid = '$user->instrument'");
    /* Lager en array med de felt som er ønskelig */
    $allusers[$user->ID] = array(
        'indeks' => $instr->indeks,
        'last' => $user->last_name,
        'first' => $user->first_name,
        'instrument' => $instr->feltdata,
        'nicename' => $user->user_nicename,
        'mobile' => $user->mobil,
        'email' => $user->user_email,
        'address' => $user->address,
        'fødselsdag' => $user->fødselsdag,
        'idnummer' => $user->ID
    );
}
asort($allusers);   /* sorterer brukere etter Instrument Indeks */
?>

<div class="member-list">
    <?php if (isadmin()) { ?>
    <form class="adminButtons" method="post" action="<?php the_permalink() ?>" style="margin-bottom: 20px;">
        <input name="action" type="submit" value="Nytt medlem"/>
    </form>
    <form class="adminButtons" method="post" action="/?page_id=1080">
        <input name="til_alle" type="submit" value="Send epost til alle"/>
        <input name="action" type="hidden" value="Send epost"/>
        <input name="framodul" type="hidden" value="<?php the_permalink() ?>"/>
        <input name="til-side" type="hidden" value="liste"/>
    </form>
    <hr/>
<?php
    }
    /* Henter ut en liste over alle brukere
     * Visningen her gir Profilbilde, Navn, Epost og Instrument, og er kun synlig for innloggede brukere
     */
    ?>
        <?php
        foreach ($allusers as $auser) {
            if (!user_can($auser['idnummer'], 'superadmin')) {
                ?>
            <table>
                <tr>
                    <?php if (is_user_logged_in()) { ?>
                        <td rowspan="4" style="vertical-align: top; width:75px;">
                            <?php echo get_avatar($auser['email'], $size = '75', $default = 'http://i45.tinypic.com/oky8na.jpg'); ?>
                        </td>
                    <?php } ?>
                    <td style="vertical-align: top; width:55px;">
                        <h5 style="margin:0 !important;">Navn: </h5>
                    </td>
                    <td style="vertical-align: top; padding-right: 0;">
                        <?php echo $auser['first'] . " " . $auser['last'] ?>
                    </td>
                    <?php if (is_user_logged_in()) { ?>
                        <td rowspan="4" style="vertical-align: top; width:60px; padding-left:10px; padding-right: 0;">
                            <form method="post" action="<?php the_permalink() ?>">
                                <input name="action" type="submit" value="Vis profil" style="width:80px;"/>
                                <input name="idnum" type="hidden" value="<?php echo $auser['idnummer'] ?>"/>
                            </form>
                            <form method="post" action="/?page_id=1080">
                                <input name="action" type="submit" value="Send epost" style="width:80px;"/>
                                <input name="framodul" type="hidden" value="<?php the_permalink() ?>"/>
                                <input name="idnum" type="hidden" value="<?php echo $auser['idnummer'] ?>"/>
                                <input name="til-side" type="hidden" value="liste"/>
                            </form>
                        </td>
                    <?php } ?>
                </tr>
                <?php if (is_user_logged_in()) { ?>
                <tr>
                    <td style="width:55px; vertical-align: top;">
                        <h5 style="margin:0 !important;">Epost: </h5>
                    </td>
                    <td style="vertical-align: top; padding-right: 0;">
                        <?php echo $auser['email']; ?>
                    </td>
                </tr>
                <?php } ?>
                <tr>
                    <td style="width:55px; vertical-align: top;">
                        <h5 style="margin:0 !important;">Instrument: </h5>
                    </td>
                    <td style="vertical-align: top; padding-right: 0;">
                        <?php echo $auser['instrument']; ?>
                    </td>
                </tr>
                <?php if (is_user_logged_in()) { ?>
                <tr>
                    <td style="width:55px; vertical-align: top;">
                        <h5 style="margin:0 !important;">Fødselsdag: </h5>
                    </td>
                    <td style="vertical-align: top; padding-right: 0;">
                        <?php
                        $fd = $auser['fødselsdag'];
                        $år = intval(substr($fd, 0, 4));
                        $md = zeroise(intval(substr($fd, 5, 2)), 2);
                        $dg = zeroise(intval(substr($fd, 8, 2)), 2); ?>
                        <?php echo "$dg.$md.$år"; ?>
                    </td>
                </tr>
                <?php } ?>
            </table>
            <?php }
        }
        ?>
</div><!--end member_list class-->

